﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace Simple.WebApi.Controllers.System;

/// <summary>
/// 标准管理
/// </summary>
[Route("QHSEHavStandard/[action]")]
[ApiController]
public class QHSEHavStandardController : ControllerBase
{
    private readonly QHSEHavStandardService _qhseHavStandardService;

    public QHSEHavStandardController(QHSEHavStandardService qHSEHavStandardService)
    {
        _qhseHavStandardService = qHSEHavStandardService;
    }

    /// <summary>
    /// 标准列表
    /// </summary>
    /// <returns></returns>
    [HttpGet]
    [CustomAuthorize("hdv_qhse_standard_list")]
    public async Task<AppResult> List()
    {
        List<QHSEHavStandardModel> data = await _qhseHavStandardService.GetAsync();
        return AppResult.Status200OK(data: data);
    }

    /// <summary>
    /// 标准查询
    /// </summary>
    /// <param name="model"></param>
    /// <returns></returns>
    [HttpGet]
    [CustomAuthorize("hdv_qhse_standard_page")]
    public async Task<AppResult> Page([FromQuery] PageInputModel model)
    {
        PageResultModel<QHSEHavStandardModel> data = await _qhseHavStandardService.GetPageAsync(model);
        return AppResult.Status200OK(data: data);
    }

    /// <summary>
    /// 标准增加
    /// </summary>
    /// <param name="model"></param>
    /// <returns></returns>
    [HttpPost]
    [CustomAuthorize("hdv_qhse_standard_insert")]
    public async Task<AppResult> Add([FromBody] QHSEHavStandardModel model)
    {
        await _qhseHavStandardService.AddAsync(model);
        return AppResult.Status200OK("新增成功");
    }

    /// <summary>
    /// 标准编辑
    /// </summary>
    /// <param name="model"></param>
    /// <returns></returns>
    [HttpPost]
    [CustomAuthorize("hdv_qhse_standard_edit")]
    public async Task<AppResult> Edit([FromBody] QHSEHavStandardModel model)
    {
        await _qhseHavStandardService.UpdateAsync(model);
        return AppResult.Status200OK("更新成功");
    }

    /// <summary>
    /// 标准删除
    /// </summary>
    /// <param name="models"></param>
    /// <returns></returns>
    [HttpPost]
    [CustomAuthorize("hdv_qhse_standard_delete")]
    public async Task<AppResult> Delete([FromBody] List<IdInputModel> models)
    {
        await _qhseHavStandardService.DeleteAsync(models.Select(m => m.Id));
        return AppResult.Status200OK("删除成功");
    }
    /// <summary>
    /// 标准下载
    /// </summary>
    /// <param name="inputID"></param>
    /// <returns></returns>
    [HttpPost]
    [CustomAuthorize("hdv_qhse_standard_list")]
    public async Task<IActionResult> DownloadPdf([FromBody] IdInputModel inputID)
    {
        var fileBytes = await _qhseHavStandardService.DownloadPdf(inputID);
        var contentType = "application/pdf";
        var fileName = inputID.Id+".pdf";
        return File(fileBytes, contentType,fileName);
    }
    /// <summary>
    /// 标准预览
    /// </summary>
    /// <param name="inputID"></param>
    /// <returns></returns>
    [HttpPost]
    [CustomAuthorize("hdv_qhse_standard_list")]
    public async Task<IActionResult> PreviewPdf([FromBody] IdInputModel inputID)
    {
        var bytes = await _qhseHavStandardService.PreviewPdf(inputID);
        var contentType = "application/pdf";
        if (bytes == null || bytes.Length == 0)
            return NotFound();
        var memoryStream = new MemoryStream(bytes);
        return new FileStreamResult(memoryStream, contentType);

    }
}
